package cn.likun.kit.file;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

import cn.likun.kit.Callable;
import cn.likun.kit.easyparam.ParamMap;
import cn.likun.kit.stream.StreamCommons;

/**
 * <p>org.apache.ant</p>
 * <p>zip 操作通用工具类</p>
 */
public class ZipCommons {
	
	public static final String ZIP_STREAM = "ZIP_STREAM" ;
	/**
	 * <p>解压zip</p>
	 * @param out 输出流
	 * @param charsetName 编码
	 * @param callable  回调函数 , 真正去执行解压操作的调用
	 * @throws Exception
	 */
	public static void packOutput(OutputStream out ,String charsetName, Callable callable) throws Exception{
		ZipOutputStream zipOut = null ;
		try {
			zipOut = new ZipOutputStream(out);
			zipOut.setEncoding(charsetName);
			callable.call(new ParamMap<String,Object>().putParam(ZIP_STREAM, zipOut));
			zipOut.flush();
		} finally {
			if (null != zipOut) {
				zipOut.close();
			}
		}
		
	}
	/**
	 * <p>打包文件为zip</p>
	 * @param entryName 文件名
	 * @param in 输入流
	 * @param out 输出流 , 方法内部没有关闭输出流 ,使用后请自行关闭
	 * @throws IOException
	 */
	public static void packFile(String entryName, InputStream in ,ZipOutputStream out) throws IOException {
		try {
			out.putNextEntry(new ZipEntry(entryName));
			StreamCommons.binaryTransport(in, out,false);
		}  finally {
			if (null != out) {
				out.closeEntry();
			}
		}
	}
	
	
	
}




























